﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using HUGATE.ItemObject;
using CoreLibs;
using HUGATE.Utility;

namespace HUGATE.Item.Controls.Manufacturer
{
    /// <summary>
    /// Creator : thai huy
    /// Date    : 11/12/2010
    /// Desc    : Quản lý nhà sản xuất
    /// </summary>
    public partial class ManufacturerEdit : System.Web.UI.UserControl
    {
        #region Variables

        private string strListPage = "Manufacturer/Manufacturer.aspx";
        private int intManufacturerID = 0;

        #endregion

        #region Properties

        public string ListPage
        {
            get { return strListPage; }
            set { strListPage = value; }
        }

        public int ManufacturerID
        {
            get { return intManufacturerID; }
            set { intManufacturerID = value; }
        }

        #endregion

        #region Permissions

        String strAddPermission = "ITEM_Manufacturer_Add";
        String strEditPermission = "ITEM_Manufacturer_Edit";
        String strDeletePermission = "ITEM_Manufacturer_Delete";

        #endregion

        #region Support Functions

        /// <summary>
        /// Reset Control's data
        /// </summary>
        public void Clear()
        {
            txtObjectID.Text = "";
            txtObjectName.Text = "";
            txtDesc.Text = "";
            chkIsDefault.Checked = false;
            chkIsDefault.Enabled = false;
            chkIsActived.Checked = false;            
        }

        /// <summary>
        /// Enable/Disable controls
        /// </summary>
        /// <param name="bolEnable"></param>
        private void EnableControl(bool bolEnable)
        {
            txtObjectName.Enabled = bolEnable;
            txtDesc.Enabled = bolEnable;
            chkIsDefault.Enabled = bolEnable;
            chkIsActived.Enabled = bolEnable;
            chkIsActived_CheckedChanged(null, null);

            udpEdit.Update();
        }

        /// <summary>
        /// Load Object's info to controls
        /// </summary>
        public void LoadInfo()
        {
            ITEMManufacturer objManufacturer = new ITEMManufacturer();
            objManufacturer.ManufacturerID = intManufacturerID;

            if (objManufacturer.LoadInfo())
            {
                txtObjectID.Text = objManufacturer.ManufacturerID.ToString();
                txtObjectName.Text = objManufacturer.ManufacturerName.Trim();
                txtDesc.Text = objManufacturer.Description.Trim();
                chkIsDefault.Checked = objManufacturer.IsDefault;
                chkIsDefault.Enabled = objManufacturer.IsActived; 
                chkIsActived.Checked = objManufacturer.IsActived;              

                
            }
        }

        #endregion

        #region Event Functions

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                btnCancel.Text = HUGATE.Utility.ResxManager.GetString("Btn_Cancel");
                btnUpdate.Text = HUGATE.Utility.ResxManager.GetString("Btn_Save");
                txtDesc.MaxLength = 2000;
                txtObjectName.MaxLength = 200;
                txtObjectName.Attributes.Add("onblur", "CheckHTML('" + txtObjectName.ClientID + "');");
                txtDesc.Attributes.Add("onblur", "CheckHTML('" + txtDesc.ClientID + "');");
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            ITEMManufacturer objManufacturer = new ITEMManufacturer();
            objManufacturer.ManufacturerName = txtObjectName.Text.Trim();
           
            objManufacturer.Description = txtDesc.Text.Trim();
            objManufacturer.IsDefault = chkIsDefault.Checked;
            objManufacturer.IsActived = chkIsActived.Checked;
            objManufacturer.CreatedUser = "administrator";

            if (txtObjectID.Text.Trim().Length > 0)
            {
                objManufacturer.ManufacturerID = Convert.ToInt32(txtObjectID.Text.Trim());
                if (objManufacturer.Update())
                {

                    GlobalFunction.ShowMessageBox(Page, string.Format(HUGATE.Utility.ResxManager.GetString("UpdateItemSuccessful"), HUGATE.Utility.ResxManager.GetString("ITEMManufacturer", true).ToLower()), HUGATE.Utility.ResxManager.GetString("MessageBox"), GlobalFunction.MessageBoxIcon.Information);
                    RaisePostBackEvent(e, "0");
                }
                else
                    GlobalFunction.ShowMessageBox(Page, string.Format(HUGATE.Utility.ResxManager.GetString("CouldNotUpdateItem"), HUGATE.Utility.ResxManager.GetString("ITEMManufacturer", true).ToLower()), HUGATE.Utility.ResxManager.GetString("MessageBox"), GlobalFunction.MessageBoxIcon.WarningMessage);
            }
            else
            {
                string tam = objManufacturer.Insert();
                if (tam != string.Empty)
                {
                    GlobalFunction.ShowMessageBox(Page, string.Format(HUGATE.Utility.ResxManager.GetString("InsertItemSuccessful"), HUGATE.Utility.ResxManager.GetString("ITEMManufacturer", true).ToLower()), HUGATE.Utility.ResxManager.GetString("MessageBox"), GlobalFunction.MessageBoxIcon.Information);
                    RaisePostBackEvent(e, tam);
                }
                else
                    GlobalFunction.ShowMessageBox(Page, string.Format(HUGATE.Utility.ResxManager.GetString("CouldNotInsertItem"), HUGATE.Utility.ResxManager.GetString("ITEMManufacturer", true).ToLower()), HUGATE.Utility.ResxManager.GetString("MessageBox"), GlobalFunction.MessageBoxIcon.WarningMessage);
            }
            Clear();


        }
        
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Clear();
            RaisePostBackEvent(e,"0");
        }      
        protected void chkIsActived_CheckedChanged(object sender, EventArgs e)
        {       
            chkIsDefault.Enabled = chkIsActived.Checked && chkIsActived.Enabled;
            if (!chkIsActived.Checked)
                chkIsDefault.Checked = false;
        }
        #endregion

        #region Event Handler

        public delegate void SaveDelegate(object sender, EventArgs e, string manufactuerID);
        public event SaveDelegate SavedEvent = null;

        protected void RaisePostBackEvent(EventArgs e, string manufactuerID )
        {
            if (SavedEvent != null)
                SavedEvent(this, e , manufactuerID);
        }
        #endregion
    }
}